{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: './reviews.csv'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mFileNotFoundError\u001B[0m                         Traceback (most recent call last)",
      "\u001B[1;32md:\\Temp\\ipykernel_17812\\2592184144.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      8\u001B[0m \u001B[0mword\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mpd\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mread_csv\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"./word.csv\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      9\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 10\u001B[1;33m \u001B[0mreviews\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mpd\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mread_csv\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'./reviews.csv'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     11\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     12\u001B[0m \u001B[0mreviews\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mreviews\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mdrop_duplicates\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msubset\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'content'\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'content_type'\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\util\\_decorators.py\u001B[0m in \u001B[0;36mwrapper\u001B[1;34m(*args, **kwargs)\u001B[0m\n\u001B[0;32m    309\u001B[0m                     \u001B[0mstacklevel\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mstacklevel\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    310\u001B[0m                 )\n\u001B[1;32m--> 311\u001B[1;33m             \u001B[1;32mreturn\u001B[0m \u001B[0mfunc\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m*\u001B[0m\u001B[0margs\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;33m**\u001B[0m\u001B[0mkwargs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    312\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    313\u001B[0m         \u001B[1;32mreturn\u001B[0m \u001B[0mwrapper\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\io\\parsers\\readers.py\u001B[0m in \u001B[0;36mread_csv\u001B[1;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)\u001B[0m\n\u001B[0;32m    676\u001B[0m     \u001B[0mkwds\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mupdate\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mkwds_defaults\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    677\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 678\u001B[1;33m     \u001B[1;32mreturn\u001B[0m \u001B[0m_read\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mfilepath_or_buffer\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mkwds\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    679\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    680\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\io\\parsers\\readers.py\u001B[0m in \u001B[0;36m_read\u001B[1;34m(filepath_or_buffer, kwds)\u001B[0m\n\u001B[0;32m    573\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    574\u001B[0m     \u001B[1;31m# Create the parser.\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 575\u001B[1;33m     \u001B[0mparser\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mTextFileReader\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mfilepath_or_buffer\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;33m**\u001B[0m\u001B[0mkwds\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    576\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    577\u001B[0m     \u001B[1;32mif\u001B[0m \u001B[0mchunksize\u001B[0m \u001B[1;32mor\u001B[0m \u001B[0miterator\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\io\\parsers\\readers.py\u001B[0m in \u001B[0;36m__init__\u001B[1;34m(self, f, engine, **kwds)\u001B[0m\n\u001B[0;32m    930\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    931\u001B[0m         \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mhandles\u001B[0m\u001B[1;33m:\u001B[0m \u001B[0mIOHandles\u001B[0m \u001B[1;33m|\u001B[0m \u001B[1;32mNone\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;32mNone\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 932\u001B[1;33m         \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_engine\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_make_engine\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mf\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mengine\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    933\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    934\u001B[0m     \u001B[1;32mdef\u001B[0m \u001B[0mclose\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\io\\parsers\\readers.py\u001B[0m in \u001B[0;36m_make_engine\u001B[1;34m(self, f, engine)\u001B[0m\n\u001B[0;32m   1214\u001B[0m             \u001B[1;31m# \"Union[str, PathLike[str], ReadCsvBuffer[bytes], ReadCsvBuffer[str]]\"\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1215\u001B[0m             \u001B[1;31m# , \"str\", \"bool\", \"Any\", \"Any\", \"Any\", \"Any\", \"Any\"\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 1216\u001B[1;33m             self.handles = get_handle(  # type: ignore[call-overload]\n\u001B[0m\u001B[0;32m   1217\u001B[0m                 \u001B[0mf\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1218\u001B[0m                 \u001B[0mmode\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\io\\common.py\u001B[0m in \u001B[0;36mget_handle\u001B[1;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001B[0m\n\u001B[0;32m    784\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0mioargs\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mencoding\u001B[0m \u001B[1;32mand\u001B[0m \u001B[1;34m\"b\"\u001B[0m \u001B[1;32mnot\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mioargs\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mmode\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    785\u001B[0m             \u001B[1;31m# Encoding\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 786\u001B[1;33m             handle = open(\n\u001B[0m\u001B[0;32m    787\u001B[0m                 \u001B[0mhandle\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    788\u001B[0m                 \u001B[0mioargs\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mmode\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: './reviews.csv'"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import re\n",
    "import jieba.posseg as psg\n",
    "\n",
    "#word = pd.read_csv(\"./word.csv\")\n",
    "\n",
    "reviews = pd.read_csv('./reviews.csv')\n",
    "\n",
    "reviews = reviews.drop_duplicates(subset=['content', 'content_type'])\n",
    "content = reviews[\"content\"]\n",
    "# 去除英文、数字、京东、美的、电热水器等词语,pattern\n",
    "strinfo = re.compile('[0-9a-zA-Z]|京东|美的|电热水器|热水器|')\n",
    "content = content.apply(lambda x: strinfo.sub('', x))\n",
    "# 分词\n",
    "worker = lambda s: [(x.word, x.flag) for x in psg.cut(s)]  # 自定义简单分词函数\n",
    "seg_word = content.apply(worker)\n",
    "# 删除停用词\n",
    "stop_path = open(\"./stoplist.txt\", 'r', encoding='UTF-8')\n",
    "stop = stop_path.readlines()\n",
    "stop = [x.replace('\\n', '') for x in stop]\n",
    "# 遍历所有词，取出停用词并选出名词，统计词频\n",
    "word_posneg = pd.DataFrame(columns=['index_content', 'word', 'nature', 'content_type', 'index_word'\n",
    "                                    ])\n",
    "index_content = 0\n",
    "for word_set in seg_word:\n",
    "    index_content += 1\n",
    "    index_word = 0\n",
    "    for w in word_set:\n",
    "        #  index_word += 1\n",
    "        if w[0] not in stop and 'n' in w[1]:\n",
    "            index_word += 1\n",
    "            # DataFrame每行要添加的Series\n",
    "            # word_series = pd.Series(\n",
    "            #     [index_content, w[0], w[1], reviews.iloc[index_content - 1][\"content_type\"], index_word])\n",
    "            # word_posneg = pd.concat([word_posneg, word_series], axis=0, ignore_index=True)\n",
    "            word_posneg.loc[len(word_posneg)] = [index_content, w[0], w[1], reviews.iloc[index_content - 1][\"content_type\"], index_word]\n",
    "\n",
    "# 读入正面、负面情感评价词\n",
    "pos_comment = pd.read_csv(\"./正面评价词语（中文）.txt\", header=None, sep=\"/n\",\n",
    "                          encoding='utf-8', engine='python')\n",
    "neg_comment = pd.read_csv(\"./负面评价词语（中文）.txt\", header=None, sep=\"/n\",\n",
    "                          encoding='utf-8', engine='python')\n",
    "pos_emotion = pd.read_csv(\"./正面情感词语（中文）.txt\", header=None, sep=\"/n\",\n",
    "                          encoding='utf-8', engine='python')\n",
    "neg_emotion = pd.read_csv(\"./负面情感词语（中文）.txt\", header=None, sep=\"/n\",\n",
    "                          encoding='utf-8', engine='python')\n",
    "\n",
    "# 合并情感词与评价词\n",
    "positive = set(pos_comment.iloc[:, 0]) | set(pos_emotion.iloc[:, 0])\n",
    "negative = set(neg_comment.iloc[:, 0]) | set(neg_emotion.iloc[:, 0])\n",
    "\n",
    "# 正负面情感词表中相同的词语\n",
    "intersection = positive & negative\n",
    "\n",
    "positive = list(positive - intersection)\n",
    "negative = list(negative - intersection)\n",
    "\n",
    "positive = pd.DataFrame({\"word\": positive,\n",
    "                         \"weight\": [1] * len(positive)})\n",
    "negative = pd.DataFrame({\"word\": negative,\n",
    "                         \"weight\": [-1] * len(negative)})\n",
    "\n",
    "posneg = pd.concat([positive, negative], ignore_index=True)\n",
    "\n",
    "\n",
    "# 将分词结果与正负面情感词表合并，定位情感词\n",
    "data_posneg = posneg.merge(word_posneg, left_on='word', right_on='word',\n",
    "                           how='right')\n",
    "# data_posneg = data_posneg.sort_values(by = ['index_content','index_word'])\n",
    "\n",
    "data_posneg.head()\n",
    "# 查看原来该句评论问pos，但其中分词后词情感标注未负面的\n",
    "data_posneg[(data_posneg[\"content_type\"]=='pos')&(data_posneg[\"weight\"]<1)]\n",
    "data_posneg[(data_posneg[\"content_type\"]=='neg')&(data_posneg[\"weight\"]==1)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>word</th>\n",
       "      <th>weight</th>\n",
       "      <th>index_content</th>\n",
       "      <th>nature</th>\n",
       "      <th>content_type</th>\n",
       "      <th>index_word</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>东西</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>ns</td>\n",
       "      <td>pos</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>品牌</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>n</td>\n",
       "      <td>pos</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>信赖</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>n</td>\n",
       "      <td>pos</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>东西</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>ns</td>\n",
       "      <td>pos</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>整体</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>n</td>\n",
       "      <td>pos</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  word  weight  index_content nature content_type  index_word\n",
       "0   东西     NaN              1     ns          pos           1\n",
       "1   品牌     NaN              1      n          pos           2\n",
       "2   信赖     1.0              1      n          pos           3\n",
       "3   东西     NaN              1     ns          pos           4\n",
       "4   整体     NaN              1      n          pos           5"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_posneg.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32md:\\Temp\\ipykernel_2132\\4155577072.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[0mpath\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;34m\"./\"\u001B[0m  \u001B[1;31m# assuming the not.csv file is in the same directory as this script\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 3\u001B[1;33m \u001B[0mdata_posneg\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mpd\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mread_csv\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"./data_posneg.csv\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      4\u001B[0m \u001B[1;31m# 载入否定词表\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[0mnotdict\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mpd\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mread_csv\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m\"not.csv\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mNameError\u001B[0m: name 'pd' is not defined"
     ]
    }
   ],
   "source": [
    "path = \"./\"  # assuming the not.csv file is in the same directory as this script\n",
    "\n",
    "data_posneg = pd.read_csv(\"./data_posneg.csv\")\n",
    "# 载入否定词表\n",
    "notdict = pd.read_csv(path+\"not.csv\")\n",
    "\n",
    "# 构造新列，作为经过否定词修正后的情感值\n",
    "data_posneg['amend_weight'] = data_posneg['weight']\n",
    "data_posneg['id'] = np.arange(0, len(data_posneg))\n",
    "\n",
    "# 只保留有情感值的词语\n",
    "only_inclination = data_posneg.dropna().reset_index(drop=True)\n",
    "\n",
    "index = only_inclination['id']\n",
    "\n",
    "\n",
    "for i in np.arange(0, len(only_inclination)):\n",
    "    # 提取第i个情感词所在的评论\n",
    "    review = data_posneg[data_posneg['index_content'] == only_inclination['index_content'][i]]\n",
    "    review.index = np.arange(0, len(review))\n",
    "    # 第i个情感值在该文档的位置\n",
    "    affective = only_inclination['index_word'][i]\n",
    "    if affective == 1:\n",
    "        ne = sum([i in notdict['term'] for i in review['word'][affective - 1]])%2\n",
    "        if ne == 1:\n",
    "            data_posneg['amend_weight'][index[i]] = -data_posneg['weight'][index[i]]          \n",
    "    elif affective > 1:\n",
    "        ne = sum([i in notdict['term'] for i in review['word'][[affective - 1, \n",
    "                  affective - 2]]])%2\n",
    "        if ne == 1:\n",
    "            data_posneg['amend_weight'][index[i]] = -data_posneg['weight'][index[i]]\n",
    "            \n",
    "\n",
    "            \n",
    "# 更新只保留情感值的数据\n",
    "only_inclination = only_inclination.dropna()\n",
    "\n",
    "# 计算每条评论的情感值\n",
    "emotional_value = only_inclination.groupby(['index_content'],\n",
    "                                           as_index=False)['amend_weight'].sum()\n",
    "\n",
    "# 去除情感值为0的评论\n",
    "emotional_value = emotional_value[emotional_value['amend_weight'] != 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
